enum VARS {
    ps1,
    ps2,
    ps3,
    ps4
}

enum CLOCKWISEE {
    //% block="正转"
    True,
    //% block="反转"
    False
}


//% color="#9bf28c" iconWidth=50 iconHeight=40
namespace MicrobluePwmServo{
    
    //% block="PWM舵机[V]，初始化: 引脚=[PIN]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.ps1"
    //% PIN.shadow="dropdownRound" PIN.options="PINS" PIN.defl=PINS.P0

    export function motor_init(parameter: any, block: any) {
        let v = parameter.V.code; 
        let pin = parameter.PIN.code;
        Generator.addImport("from mb_pwm_servo import DsPwmServo", true);
        Generator.addCode(`${v} = DsPwmServo(pin=${pin})`);
    }

  
    //% block="270度舵机[V]：角度: [ANGLE]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.ps1"
    //% ANGLE.shadow="number" ANGLE.defl=270

    export function servo_270(parameter: any, block: any) {
        let v = parameter.V.code; 
        let angle=parameter.ANGLE.code;
        Generator.addCode(`${v}.servo_270(${angle})`);
    }
    
    //% block="180度舵机[V]：角度: [ANGLE]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.ps1"
    //% ANGLE.shadow="number" ANGLE.defl=180

    export function servo_180(parameter: any, block: any) {
        let v = parameter.V.code; 
        let angle=parameter.ANGLE.code;
        Generator.addCode(`${v}.servo_180(${angle})`);
    }


    //% block="360度电机: 速度=[SPEED], 方向=[DIR]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.ps1"
    //% SPEED.shadow="number" SPEED.min=0 SPEED.max=100  SPEED.defl=100
    //% DIR.shadow="dropdownRound" DIR.options="CLOCKWISEE" DIR.defl="CLOCKWISEE.True"

    export function servo_360(parameter: any, block: any) {
        let v = parameter.V.code; 
        let speed=parameter.SPEED.code;
        let direction=parameter.DIR.code;
        Generator.addCode(`${v}.motor_run(${speed}, ${direction})`);
    }
}
